<?php

/**
* alltosun.com  index.php for newbd
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* Author: 吴松键 (wusj@alltosun.com)
* Date: 15/1/21 下午6:08
*/

class Action
{
    private $per_page = 20;

    public function index()
    {
        $page = Request::Get('page_no', 1);
        $status = Request::Get('status', 1);
        $search_filter = Request::getParam('search_filter', array());

        if (isset($search_filter['title']) && $search_filter['title']) {
            $filter['`title` LIKE '] = '%' . $search_filter['title'] . '%';
        }
        $filter['status'] = $status;

        $list = array();
        $count = _model('promotion_sub')->getTotal($filter);
        if ($count) {
            $pager = new Pager($this->per_page);
            if ($pager->generate($goods_count)) {
                Response::assign('pager', $pager);
            }
            $order = ' ORDER BY `add_time` DESC ';

            $list = _model('promotion_sub')->getList($filter, $order . $pager->getLimit($page));
        }

        Response::assign('list', $list);
        Response::assign('status', $status);
        Response::assign('search_filter', $search_filter);

        Response::display('admin/list.html');
    }

    public function add()
    {
        $id = Request::getParam('id', 0);

        if ($id) {
            $promotion_sub_info = _uri('promotion_sub', $id);
            Response::assign('promotion_info', $promotion_sub_info);
        }

        Response::display('admin/add.html');
    }

    public function save()
    {
        $id = Request::getParam('id', 0);
        $info = Request::getParam('info', array());

        foreach ($info as $k=>$v) {
            if (!is_numeric($v)) {
                $info[$k] = trim(AnFilter::filter_string($v));
            }
        }

        if ($id) {
            $promotion_sub_info = _uri('promotion_sub', $id);
            if (!$promotion_sub_info) {
                return '该内容不存在';
            }

            _model('promotion_sub')->update($id, $info);
        } else {
            _model('promotion_sub')->create($info);
        }

        Response::redirect(AnUrl('promotion_sub/admin'));
    }

    /**
     * 删除
     * @return array
     */
    public function delete()
    {
        $id = Request::getParam('id');

        if (!$id) {
            return '请选择要删除的内容';
        }

        $ids = explode(',', trim($id, ','));
        foreach ($ids as $k=>$v) {
            if (!$v) {
                continue;
            }

            $promotion_sub_info = _uri('promotion_sub', (int)$v);
            if (!$promotion_sub_info) {
                continue;
            }

            if (1 == $promotion_sub_info['status']) {
                _model('promotion_sub')->update($v, array('status'=>0));
            } else {
                _model('promotion_sub')->delete($v);
            }
        }

        return 'ok';
    }

    /**
     * 更新状态
     * @return array
     */
    public function change_status()
    {
        $id     = Request::getParam('id', 0);
        $status = Request::getParam('status', 1);
        if (!$id) {
            return '请选择要更改的内容';
        }

        $promotion_sub_info = _uri('promotion_sub', $id);
        if ($status == $promotion_sub_info['status']) {
            return '已经执行过此操作，不用重复提交';
        }

        if (1 == $status) {
            _model('promotion_sub')->update($id, array('status'=>1));
        }

        return 'ok';
    }
}